package com.ruoyi.system.controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.dto.AudiDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Audi;
import com.ruoyi.system.service.IAudiService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.dto.SignInRequest;
/**
 * 观众报名Controller
 * 
 * @author saruri
 * @date 2024-06-18
 */
@RestController
@RequestMapping("/audi")
public class AudiController extends BaseController
{
    @Autowired
    private IAudiService audiService;

    @Autowired
    private ISysUserService userService;
    /**
     * 查询观众报名列表
     */
    @RequiresPermissions("system:audi:list")
    @GetMapping("/list")
    public TableDataInfo list(Audi audi)
    {
        startPage();
        List<Audi> list = audiService.selectAudiList(audi);
        return getDataTable(list);
    }

    @GetMapping("/getProfileByUserAndTv")
    public AjaxResult getProfileByUserAndTv(@RequestParam("userId") Long userId, @RequestParam("tvId") Long tvId) {
        AjaxResult ajax = AjaxResult.success();
        Audi audi = audiService.selectAudiByUserIdAndTvId(userId, tvId);
        if (audi != null)
        {
            ajax.put(AjaxResult.DATA_TAG, audi);
        }
        else
        {
            ajax.put(AjaxResult.DATA_TAG, "No data found");
        }
        return ajax;
    }

    @PutMapping("/updateProfileByUserAndTv")
    public AjaxResult updateProfileByUserAndTv(@RequestBody Audi audi) {
        int result = audiService.updateAudiByUserIdAndTvId(audi);
        return toAjax(result);
    }

    /**
     * 导出观众报名列表
     */
    @RequiresPermissions("system:audi:export")
    @Log(title = "观众报名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Audi audi)
    {
        List<Audi> list = audiService.selectAudiList(audi);
        ExcelUtil<Audi> util = new ExcelUtil<Audi>(Audi.class);
        util.exportExcel(response, list, "观众报名数据");
    }

    /**
     * 获取观众报名详细信息
     */
    @RequiresPermissions("system:audi:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(audiService.selectAudiById(id));
    }

    /**
     * 新增观众报名
     */
    @CrossOrigin(origins = "http://localhost:9090") // 允许的前端地址
    @RequiresPermissions("system:audi:add")
    @Log(title = "观众报名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Audi audi)
    {
        return toAjax(audiService.insertAudi(audi));
    }

    /**
     * 新增观众报名
     */

    @RequiresPermissions("system:audi:add")
    @Log(title = "观众报名", businessType = BusinessType.INSERT)
    @PostMapping("addAudi")
    public AjaxResult addAudi(@RequestBody Audi audi)
    {
        System.out.println("Received POST request with audi: " + audi.toString());
        if (audi.getName() == null || audi.getName().isEmpty()) {
            return AjaxResult.error("Name is required");
        }
        System.out.println("Received POST request with audi: " + audi);
        return toAjax(audiService.insertAudi(audi));
    }

    /**
     * 用户签到
     */

    @RequiresPermissions("system:audi:add")
    @Log(title = "观众签到", businessType = BusinessType.OTHER)
    @PostMapping("signIn")
    public AjaxResult signin(@RequestBody SignInRequest signInRequest) {
        SysUser user = userService.selectUserById(SecurityUtils.getUserId());
        System.out.println("用户id: " + SecurityUtils.getUserId());


        if ( signInRequest.getTvId() == null ||  signInRequest.getEndTime() == null) {
            return AjaxResult.error("tvId and endTime are required");
        }

        signInRequest.setUserId(SecurityUtils.getUserId());
        signInRequest.setIsSignedIn(1);
        signInRequest.setSignInType(1);
        signInRequest.setSignInTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println("audi的: " + signInRequest.toString());
        int result = audiService.signIn(signInRequest);
        //System.out.println("caozuo " +result);
        return toAjax(result);
    }

    /**
     * 用户签到列表
     */

    @RequiresPermissions("system:audi:add")
    @GetMapping("/signed")
    public AjaxResult signed() {
        //startPage();
        //AjaxResult ajax = AjaxResult.success();
        Long userId = SecurityUtils.getUserId();

        // 从服务层获取签到列表
        List<AudiDTO> list =   audiService.selectAudiByUserId(userId);

        // 包装结果并返回
        return AjaxResult.success(list);
    }


    /**
     * 修改观众报名
     */
    @RequiresPermissions("system:audi:edit")
    @Log(title = "观众报名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Audi audi)
    {
        return toAjax(audiService.updateAudi(audi));
    }

    /**
     * 删除观众报名
     */
    @RequiresPermissions("system:audi:remove")
    @Log(title = "观众报名", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(audiService.deleteAudiByIds(ids));
    }
}
